<style>
.content {
    padding:10px 10px;
}
 /* 人员节点 展开、折叠*/
 .ztree li span.button.diy01_ico_open, .ztree li span.button.diy01_ico_close{
        background-image: url('/start/plugin/zTree/css/zTreeStyle/img/icon-role.png');
    }
    /* 父节点展开 */
    .ztree li span.button.diy02_ico_open{
        background-image: url('/start/plugin/zTree/css/zTreeStyle/img/zTreeStandard.png');
        background-position: -110px -16px;
    }
    /* 父节点折叠 */
    .ztree li span.button.diy02_ico_close{
        background-image: url('/start/plugin/zTree/css/zTreeStyle/img/zTreeStandard.png');
        background-position: -110px 0;
    }

    /* 子节点 */
    .ztree li span.button.diy02_ico_docu{
        background-image: url('/start/plugin/zTree/css/zTreeStyle/img/zTreeStandard.png');
        background-position: -110px 0;
    }
    .ztree li span.button.diy01_ico_docu{
        background-image: url('/start/plugin/zTree/css/zTreeStyle/img/icon-role.png');
    }
    .ztree .no-data-tips{
        margin-top: 100px;
    }
    .ztree{
        width: fit-content; 
        margin-left: 102px;
    }
    .ztree li span{
        font-size: 16px;
    }
    textarea[name="usermsg"]{
        height: 40px;
    }
</style>
<!-- <link href="/start/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet"/> 
<script src="/start/plugin/jq-1.6.4.min.js"></script>
<script src="/start/plugin/zTree/js/jquery.ztree.core.js"></script>
<script src="/start/plugin/zTree/js/jquery.ztree.excheck.js"></script>  -->
<script type="text/html" template lay-done="layui.data.fillauditInfo(d);">
    <div class="content">
        <form class="layui-form" action="">  
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">审核人</label>
                <div class="layui-input-block">
                  <input name="usermsg" placeholder="请选择审核人" class="layui-input" readonly="readonly">
                  <input style="display:none;" name="usermsgdata" type="text">
                </div>
              </div> 
        </form>
        <!-- <table class="c-checkTable" id="checkTable" lay-filter="f-checkTable"></table> -->
        <ul id="treeDemo" class="ztree"></ul>
        <form class="layui-form" action="">  
            <div class="layui-form-item layui-form-text">
              <label class="layui-form-label">备注</label>
              <div class="layui-input-block">
              <textarea name="memo" placeholder="请输入备注" class="layui-textarea"></textarea>
              </div>
          </div>
      </form>
    </div>
</script>
<script>
    layui.data.fillauditInfo = function (d) {
        layui.use(['admin', 'form', 'table', 'tools'], function(){
            var //$ = layui.$,
                admin=layui.admin,
                form = layui.form,
                table = layui.table,
                tools = layui.tools,
                nowData = [],
                checkData = [];
                var treeCheckPersonObj={};
                var treeCheckedPersonArr=[];
                var treeCheckFlag=false;

                //渲染树
            var setting = {
                data: {
                    key: {
                        name: 'nodename',
                        // checked: "isChecked"
                    },
                    simpleData: {
                        enable: true,
                        idKey: "writeid",
                        pIdKey: "pId",
                        rootPId: 0
                    }
                }, 
                check:{
                    enable:true,
                    chkboxType: {"Y": "ps", "N": "ps"}
                },
                treeNode:{

                },
                callback: {
                    // onClick: zTreeOnClick,
                    onCheck:zTreeOnCheck
                }
            }; 
            //复选框选择
            function zTreeOnCheck(event, treeId, treeNode){ 
                 if(treeNode.children&&treeNode.children.length>0){
                    if(treeNode.checked){//全选 
                        treeCheckedPersonArr=[];
                        for(var i=0;i<treeNode.children.length;i++){
                            treeCheckedPersonArr.push({
                                dept_code:treeNode.children[i].dept_code,
                                ident_id:treeNode.children[i].ident_id, 
                                sex:treeNode.children[i].sex, 
                                dept_name:treeNode.children[i].dept_name, 
                                job_status:treeNode.children[i].job_status, 
                                login_name:treeNode.children[i].login_name, 
                                phone:treeNode.children[i].phone, 
                                name:treeNode.children[i].name, 
                                exist_type:treeNode.children[i].exist_type, 
                                dept_id:treeNode.children[i].dept_id, 
                                email:treeNode.children[i].email, 
                                status:treeNode.children[i].status, 

                                cert_code:treeNode.children[i].nodecode,  
                                user_code:treeNode.children[i].nodeid, 
                                user_name:treeNode.children[i].nodename
                            })
                            treeCheckPersonObj[treeNode.children[i].nodeid]=treeNode.children[i].nodeid;
                        }
                    }else{//全不选
                        treeCheckPersonObj={};
                        treeCheckedPersonArr=[];
                    }
                       
                 }else{//全部选 
                    if(treeNode.checked){//选中
                        // treeCheckPersonObj[nodeid]=childrenArr[i].nodeid;
                        if(treeCheckPersonObj[treeNode.nodeid]){//已经存在
                            treeCheckPersonObj[treeNode.nodeid]=treeNode.nodeid;
                        }else{//不存在
                            treeCheckedPersonArr.push({
                                dept_code:treeNode.dept_code,
                                ident_id:treeNode.ident_id, 
                                sex:treeNode.sex, 
                                dept_name:treeNode.dept_name, 
                                job_status:treeNode.job_status, 
                                login_name:treeNode.login_name, 
                                phone:treeNode.phone, 
                                name:treeNode.name, 
                                exist_type:treeNode.exist_type, 
                                dept_id:treeNode.dept_id, 
                                email:treeNode.email, 
                                status:treeNode.status, 

                                cert_code:treeNode.nodecode,  
                                user_code:treeNode.nodeid, 
                                user_name:treeNode.nodename
                            })
                        } 
                    }else{//删除
                        delete treeCheckPersonObj[treeNode.nodeid];
                        for(var k=0;k<treeCheckedPersonArr.length;k++){
                            if(treeCheckedPersonArr[k].user_code==treeNode.nodeid){
                                treeCheckedPersonArr.splice(k,1);
                            }
                        }
                    } 
                 }
                   
                 var perName=[];
                 for(var p=0;p<treeCheckedPersonArr.length;p++){
                    perName.push(treeCheckedPersonArr[p].user_name);
                 } 
                 $('.content').find('input[name="usermsg"]').val(perName.join(",")); 
                $('.content').find('input[name="usermsgdata"]').val(JSON.stringify(treeCheckedPersonArr));
            }
            //查询已有审核人
            admin.req({
                url: 'myapi/DC/api/rest/web/custom/GetCheckMsg',
                type:'get',
                data: {
                    model: 'adduser',
                    taskid: d.params.taskid
                },
                success: function(res){
                    checkData = res.data;
                    var str='';
                    for(var i=0;i<checkData.length;i++) {
                        str+=(checkData[i].user_name+',');
                    }
                    $('.content').find('input[name="usermsg"]').val(str);
                    $('.content').find('input[name="usermsgdata"]').val(JSON.stringify(checkData));
                    //请求树的数据 
                    getTreeFun();
                }
            }); 
            function getTreeFun(){
                admin.req({
                url: 'myapi/DC/api/rest/web/custom/GetCheckMsg',
                type:'get',
                data: {
                    model: 'deptuser',
                    access_token: layui.data('layuiAdmin').access_token, 
                },
                success: function(res){ 
                        if(res.retcode=="0"){
                            var retdata=res.data.retdata;
                            var childrenArr=retdata.child; 
                            var childrenNodes=[]; 
                            if(childrenArr.length>0){

                                for(var i=0;i<childrenArr.length;i++){
                                    for(var j=0;j<checkData.length;j++){ 
                                        if(childrenArr[i].nodeid==checkData[j].user_code){
                                            childrenArr[i].checked=true;  
                                            treeCheckPersonObj[childrenArr[i].nodeid]=childrenArr[i].nodeid;

                                            // treeCheckedPersonArr.push(childrenArr[i]);  
                                            treeCheckedPersonArr.push({
                                                dept_code:childrenArr[i].dept_code,
                                                ident_id:childrenArr[i].ident_id, 
                                                sex:childrenArr[i].sex, 
                                                dept_name:childrenArr[i].dept_name, 
                                                job_status:childrenArr[i].job_status, 
                                                login_name:childrenArr[i].login_name, 
                                                phone:childrenArr[i].phone, 
                                                name:childrenArr[i].name, 
                                                exist_type:childrenArr[i].exist_type, 
                                                dept_id:childrenArr[i].dept_id, 
                                                email:childrenArr[i].email, 
                                                status:childrenArr[i].status, 

                                                cert_code:childrenArr[i].nodecode,  
                                                user_code:childrenArr[i].nodeid, 
                                                user_name:childrenArr[i].nodename
                                            })
                                            treeCheckFlag=true;
                                        }
                                    }
                                } 
                            }
                                var zNodes=[
                                    {nodeid:retdata.nodeid,nodecode:retdata.nodecode,nodename:retdata.nodename, open:true, 
                                     children: childrenArr
                                    }
                                ] 
                            $('.content').find('input[name="usermsgdata"]').val(JSON.stringify(treeCheckedPersonArr));
                            var zTree =  $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                            var nodes = zTree.getNodes();
                            // if(nodes.length > 0){
                            //     zTree.selectNode(nodes[0]); //默认选中指定节点
                            //     if(nodes[0].checking === '1') {
                            //         getKqTaskForm(nodes[0].id, nodes[0].taskid, nodes[0].link);
                            //         pickTypeShow('kqform');
                            //     }else {

                            //        // getTaskForm(nodes[0].id, nodes[0].taskid, nodes[0].link); //默认展示第一个节点表单
                            //         leftListQuery(nodes[0].taskid);
                            //         pickTypeShow('other');
                            //     }
                            //     window.Id = nodes[0].id;
                            //     window.writeid = nodes[0].writeid;
                            //     window.taskId = nodes[0].taskid;
                            //     window.link = nodes[0].link; 
                            //     window.taskname = nodes[0].taskname;
                            //     window.checking = nodes[0].checking;
                            // }
                        }else{

                        }
                    }
                });
            }
            
            // //执行渲染
            // table.render({
            //     elem: '#checkTable', //指定原始表格元素选择器（推荐id选择器）
            //     //height: 250, //容器高度
            //     cols: [
            //         [{type:'checkbox'},
            //         {field:'name', title: '用户名'},
            //         {field:'cert_code', title: '编码',hide:true},
            //         {field:'dept_name', title: '所在单位'}
            //         ]
            //     ], //设置表头
            //     // page: true,
            //     page: false,
            //     /* page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            //         layout: ['limit', 'prev', 'page', 'next'] //自定义分页布局
            //         //,curr: 5 //设定初始在第 5 页
            //         ,groups: 1 //只显示 1 个连续页码
            //         ,first: false //不显示首页
            //         ,last: false //不显示尾页
            //     }, */
            //     url:'myapi/DC/api/rest/web/custom/GetCheckMsg?model=deptuser',
            //     limit: 5,
            //     limits: [5,10],
            //     where: {
            //         access_token: layui.data('layuiAdmin').access_token,
            //     },
            //     request: {
            //         limitName: 'count' //每页数据量的参数名，默认：limit
            //     },
            //     parseData: function(res) {
            //         nowData = res.data.retdata;
            //         for(var i=0;i<nowData.length;i++){
            //             var flag = false;
            //             for(var j=0;j<checkData.length;j++) {
            //                 if(nowData[i].cert_code === checkData[j].cert_code) {
            //                     flag = true;
            //                 }
            //             }
            //             if(!!flag) {
            //                 nowData[i].LAY_CHECKED = 'true';
            //             }
            //         }
            //         return {
            //             "code": res.retcode,
            //             "msg": res.retmsg,
            //             "count": res.data.sumNumber,
            //             "data": nowData
            //         } 
            //     }
            //     //,…… //更多参数参考右侧目录：基本参数选项
            // });

            table.on('checkbox(f-checkTable)', function(obj){
                var item = obj.data;
                if(obj.checked) {
                    if(obj.type === 'one') {
                        checkData.push({user_name:item.name,user_code: item.user_code,cert_code:item.cert_code});
                    }else if(obj.type === 'all') {
                        for(var i=0;i<nowData.length;i++){
                            checkData.push({user_name:nowData[i].name,user_code: nowData[i].user_code,cert_code:nowData[i].cert_code});
                        }
                    }
                    checkData = tools.uniqueArr(checkData,'cert_code');
                }else {
                    if(obj.type === 'one') {
                        var delData = [];
                        for(var i=0;i<checkData.length;i++) {
                            if(checkData[i].cert_code !== item.cert_code) {
                                delData.push(checkData[i]);
                            }
                        }
                        checkData = delData;
                    }else if(obj.type === 'all') {
                        var delData = [];
                        for(var i=0;i<checkData.length;i++){
                            var flag = false;
                            for(var j=0;j<nowData.length;j++) {
                                if(nowData[j].cert_code === checkData[i].cert_code) {
                                    flag = true;
                                }
                            }
                            if(!flag) {
                                delData.push(checkData[i]);
                            }
                        }
                        checkData = delData;
                    }
                }
                var str='';
                for(var i=0;i<checkData.length;i++) {
                    str+=(checkData[i].user_name+',');
                }
                $('.content').find('input[name="usermsg"]').val(str);
                $('.content').find('input[name="usermsgdata"]').val(JSON.stringify(checkData));
            });
        });
    };
</script>